#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPainter>
#include "chesswidget.h"
#include <QString>
#include <QMouseEvent>
const QString chessName[] = {"兵","炮", "车","马","象","士","将"};

enum chessType {
    soldier = 0,// 兵
    cannon,     // 炮
    car,        // 车
    horse,      // 马
    elephant,   // 象
    housecarl,  // 士
    general,    // 将
    unkonw
};

const int allChess = 32;
class chessInfo{
public:
    chessInfo():x(-1),y(-1),type(unkonw),red(false),dead(false),choosed(false)
    {}
    int x;
    int y;
    chessType type;
    QString name;
    bool red;
    bool dead;
    bool choosed;
    bool operator==(const chessInfo& a/*,const chessInfo& b*/){
        return a.x == x && a.y == y;
    }
    bool operator!=(const chessInfo& a/*,const chessInfo& b*/){
        return a.x != x || a.y != y;
    }
};

//const int gap = 25;

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
private:
    void init();
    void drawChessBoard();
    void getSuitableRect();
    void paintEvent(QPaintEvent * event);

private:
    Ui::Widget *ui;
    QRect m_rect;
    QPoint m_start;
    double scale;
    int32_t border_length;
    int32_t m_gapx;
    int32_t m_gapy;
    int m_chooseChess;
    chessType m_type;
    bool isRedTrun;

    void drawChessFrom();
    void drawChessKingArea();
    void drawChessSoldiersArea();
    //chessWidget m_chess[total][allChess];
    chessInfo chessInfos[allChess];
    void drawChesses();
    void mouseReleaseEvent(QMouseEvent *event) ;

    void getXY(const QPoint &qp, int &x, int &y);
    bool GoChesses(chessInfo chess, int dstx, int dsty);
    bool GoSoldier(chessInfo chess,int dstx,int dsty);
    bool GoCannon(chessInfo chess,int dstx,int dsty);
    bool GoCar(chessInfo chess,int dstx,int dsty);
    bool GoHorse(chessInfo chess,int dstx,int dsty);
    bool GoElephant(chessInfo chess,int dstx,int dsty);
    bool GoHousecarl(chessInfo chess,int dstx,int dsty);
    bool GoGeneral(chessInfo chess,int dstx,int dsty);
    int InOneLine(int x1, int y1, int x2, int y2);
    int CalSumXY(const chessInfo &chess, int dstx, int dsty);
    int GetFoot(chessInfo chess, int dstx, int dsty);
    int CalSumXY(int x, int y, int dstx, int dsty);
    bool CheckKing(chessInfo chess, int dstx, int dsty);
};
#endif // WIDGET_H
